package com.chenfan.common.utils;

import com.chenfan.common.constant.DeleteFlag;
import com.chenfan.common.core.entity.BusinessCreateEntity;
import com.chenfan.common.core.entity.BusinessEntity;
import com.fasterxml.uuid.Generators;
import com.fasterxml.uuid.impl.RandomBasedGenerator;
import com.fasterxml.uuid.impl.TimeBasedGenerator;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

/**
 * @author weishili
 */
public class CFUtils {

    /**
     * 生成UUID
     *
     * @return
     */
    public static String uuid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    /**
     * 基于时间生成 UUID
     *
     * @return UUID String  不带"-"
     */
    public static String generateBasedTime() {
        TimeBasedGenerator timeBasedGenerator = Generators.timeBasedGenerator();
        return timeBasedGenerator.generate().toString().replaceAll("-", "");
    }

    /**
     * 随机生成 UUID
     *
     * @return UUID String 不带"-"
     */
    public static String generateBasedRandom() {
        RandomBasedGenerator randomBasedGenerator = Generators.randomBasedGenerator();
        return randomBasedGenerator.generate().toString().replaceAll("-", "");
    }

    /**
     * 新增通用信息set
     *
     * @param entity
     * @param userId
     * @param username
     * @param cur
     */
    public static void commonInfoInsertSet(BusinessCreateEntity entity, Long userId, String username, Date cur) {
        entity.setCreateBy(userId);
        entity.setCreateName(username);
        entity.setCreateTime(cur);
    }

    /**
     * 新增通用信息set
     *
     * @param entity
     * @param userId
     * @param username
     * @param cur
     */
    public static void commonInfoInsertSet(BusinessEntity entity, Long userId, String username, Date cur) {
        entity.setCreateBy(userId);
        entity.setCreateName(username);
        entity.setCreateTime(cur);
        entity.setIsLock(DeleteFlag.EXIST);
        entity.setStatus(DeleteFlag.EXIST);
        entity.setIsDeleted(DeleteFlag.EXIST);
        commonInfoUpdateSet(entity, userId, username, cur);
    }

    /**
     * 更新通用信息set
     *
     * @param entity
     * @param userId
     * @param username
     * @param cur
     */
    public static void commonInfoUpdateSet(BusinessEntity entity, Long userId, String username, Date cur) {
        entity.setUpdateBy(userId);
        entity.setUpdateName(username);
        entity.setUpdateTime(cur);
    }

    public static List<Long> stringToLongList(String str) {
        List<Long> list = new LinkedList<>();
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        for (String d : str.split(",")) {
            list.add(Long.parseLong(d));
        }
        return list;
    }

    public static List<Integer> stringToIntegerList(String str) {
        List<Integer> list = new LinkedList<>();
        if (StringUtils.isEmpty(str)) {
            return null;
        }
        for (String d : str.split(",")) {
            list.add(Integer.parseInt(d));
        }
        return list;
    }

}
